var editRowsNumber,_datasDatagrid, selectForm;
var createDate1,createDate2,checkName,checkStatus;
var userid;
/****教师****/
var dialogTeacher,formTeacher;
var name_tea,status_tea, teacherAge,teacherWorkHourse,teacherCollage,teacherWorkYear,teacherSex,teacherXueLi,teacherNaetil,teacherAge,
teacherPro,teacherSubNum,teacherPrivate,teacherAddress,teacherScore,teacherDetail,teacherEmail,teacherQQ,teacherTel;
/******童星******/
var formTeacherReplys,dialogTeacherReplys,createDate1,createDate2,name,checkName,logo,sessionID,childrenNickName,childrenStarPro,childrenStarSex,childrenStarAge,childrenStarDream,childrenStarTrain,childrenStarIdea;
/******培训机构**********/
var dialogAddRow,formAddRow,text,status_train,address,cid,studentsNum,subscribeNum,praiseNum,score,logoPic_train,teachingScope,note;


function deleteRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				$.post(sy.bp()+"/admin/user/store/delete", {ids:ids.join(",")}, function(data) {
					if (data && data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						editRowsNumber = -1;
						_datasDatagrid.datagrid('unselectAll');
						_datasDatagrid.datagrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
};



function saveRow() {
	_datasDatagrid.datagrid('endEdit', editRowsNumber);
};


function rejectRow() {
	editRowsNumber = -1;
	_datasDatagrid.datagrid('rejectChanges');
	_datasDatagrid.datagrid('unselectAll');
};

function search_datas() {
	_datasDatagrid.datagrid('load',sy.serializeObject(selectForm));
}


function reset_datas() {
	createDate1.datebox('setValue', '');
	createDate2.datebox('setValue', '');
	$('#checkStatus').combobox('setValue','');
	$('#checkName').val('');
	_datasDatagrid.datagrid('load',{});
}

  
function lookDetails(){
	var row = _datasDatagrid.datagrid('getSelections');
	switch (row.length) {
	case 0:
		$.messager.alert('提示', '请选择要查看的收藏', 'warning');
		break;
	case 1:	
		if(row[0].status==1){
			$.post(sy.bp()+"/admin/user/store/getObj", {objid:row[0].objid,status:row[0].status}, function(data) {
				if (data && data.result==1) {
					/***机构初始化设置**/
					var rowData =data.obj;
					$('#text').val(rowData.text);
					$('#status_train').combobox('setValue', rowData.status);
					$('#address').val(rowData.address);
					$('#cid').combotree('setValue',rowData.cid);
					if(rowData.teachingScope != null){
						var array = new Array();
						var nodearr = rowData.teachingScope.split(',');
						for(var i = 0 ; i < nodearr.length; i++){
							array.push(nodearr[i]);
						}
						teachingScope.combotree('setValues', array);
					}else{
						teachingScope.combotree('setValue', '');
					}
					$('#note').html(rowData.note);
					$('#studentsNum').val(rowData.studentsNum);
					$('#subscribeNum').val(rowData.subscribeNum);
					$('#praiseNum').val(rowData.praiseNum);
					$('#score').val(rowData.score);
					if (rowData.logo) {
						$('#logoPic_train').attr('src', rowData.logo);
					} else {
						$('#logoPic_train').attr('src', sy.bp()+'/resources/css/images/logo_px.png');
					}
					dialogAddRow.dialog({title:'查看详情',iconCls:'icon-add'});
					dialogAddRow.dialog('open');
				} else {
					$.messager.alert('警告', data.msg, 'warning');
				}
			}, "json");
		}else if(row[0].status==2){
			$.post(sy.bp()+"/admin/user/store/getObj",{objid:row[0].objid,status:row[0].status}, function(data) {
				if (data && data.result==1) {
					/***教师初始化设置**/
					var rowData =data.obj;
					$('#name_tea').val(rowData.name);
					$('#teacherSex').val(rowData.teacherSex);
					$('#teacherAge').val(rowData.teacherAge);
					$('#teacherCollage').val(rowData.teacherCollage);
					$('#teacherXueLi').val(rowData.teacherXueLi);
					$('#teacherNaetil').val(rowData.teacherNaetil);
					$('#teacherWorkYear').val(rowData.teacherWorkYear);
					$('#teacherAddress').val(rowData.teacherAddress);
					$('#teacherWorkHourse').val(rowData.teacherWorkHourse);
					$('#teacherDetail').html(rowData.teacherDetail);
					$('#teacherScore').val(rowData.teacherScore);
					$('#teacherSubNum').val(rowData.teacherSubNum);
					$('#teacherPrivate').val(rowData.teacherPrivate);
					$('#status_tea').combobox('setValue',rowData.status);
					$('#teacherTel').val(rowData.teacherTel);
					$('#teacherEmail').val(rowData.teacherEmail);
					$('#teacherQQ').val(rowData.teacherQQ);
					if(rowData.teacherPro != null){
						var array = new Array();
						var nodearr = rowData.teacherPro.split(',');
						for(var i = 0 ; i < nodearr.length; i++){
							array.push(nodearr[i]);
						}
						teacherPro.combotree('setValues', array);
					}else{
						teacherPro.combotree('setValue', '');
					}
					if (rowData.logo) {
						$('#logoPic_tea').attr('src',rowData.logo);
					} else {
						$('#logoPic_tea').attr('src', sy.bp()+'/resources/css/images/logo_px.png');
					}
					dialogTeacher.dialog({title:'查看详情',iconCls:'icon-add'});
					dialogTeacher.dialog('open');
				} else {
					$.messager.alert('警告', data.msg, 'warning');
				}
			}, "json");
		}else if(row[0].status==3){
			$.post(sy.bp()+"/admin/user/store/getObj",{objid:row[0].objid,status:row[0].status}, function(data) {
				if (data && data.result==1) {
					/***教师初始化设置**/
				} else {
					$.messager.alert('警告', data.msg, 'warning');
				}
			}, "json");
		}
		break;
	default:
		$.messager.alert('提示', '您选择了多条记录数，每次只能点赞一个小童星', 'warning');
		break;
	}
}

$(function() {
	editRowsNumber = -1;
	_datasDatagrid = $('#_datasDatagrid');
	selectForm = $('#selectForm');
	userid=$('#userid').val();
	dialogTeacher=$('#dialogTeacher');
	 formTeacher=$('#formTeacher');
	 dialogAddRow=$('#dialogAddRow');
	 formAddRow=$('#formAddRow');
	createDate1 = $('#createDate1').datebox({editable:true}).datebox('activeTextArrow');
	createDate2 = $('#createDate2').datebox({editable:true}).datebox('activeTextArrow');
	/******机构初始化设置****/
	status_train = $('#status_train').val();
	address = $('#address');
	cid = $('#cid');
	logoPic_train = $('#logoPic_train');
	teachingScope = $('#teachingScope').combotree({required:true,editable:false,valueField : 'id', textField : 'text',url:sy.bp()+'/admin/courseType/listTreegrid',
		 delay:800,validType:"ctypeVertify" ,cascadeCheck:true,multiple:true }).combotree('activeTextArrow').combotree('initClear');
	$('#status_train').combobox({valueField:'code',textField:'name',editable:false,
		url:sy.bp() + "/admin/dictionary/listDictionary?name=审核状态"
	}).combobox('activeTextArrow');
	cid.combotree({required:true,editable:false,url:sy.bp()+'/admin/citys/listTree',delay:800}).combotree('activeTextArrow').combotree('initClear');
	dialogAddRow.show().dialog({resizable:false,closed:true,closable:true,modal:true});
	formAddRow.form({});
	
	/***教师初始化设置**/
	status_tea=$('#status_tea').val();
	 $('#checkStatus').combobox({valueField:'code',textField:'name',editable:false,
			url:sy.bp() + "/admin/dictionary/listDictionary?name=收藏类型"
		}).combobox('activeTextArrow');
	status_tea=$('#status_tea').combobox({valueField:'code',textField:'name',editable:false,
		url:sy.bp() + "/admin/dictionary/listDictionary?name=V等级"
	}).combobox('activeTextArrow');
	teacherPro =$('#teacherPro').combotree({required:true,editable:false,valueField : 'id', textField : 'text',url:sy.bp()+'/admin/courseType/listTreegrid',
		 delay:800,validType:"ctypeVertify" ,cascadeCheck:true,multiple:true }).combotree('activeTextArrow').combotree('initClear');
	teacherPrivate =$('#teacherPrivate').combobox({valueField:'code',textField:'name',editable:false,
		url:sy.bp() + "/admin/dictionary/listDictionary?name=是否私教"
	}).combobox('activeTextArrow');
	formTeacher.form({});
	dialogTeacher = $('#dialogTeacher').show().dialog({
		resizable : false,
		closed : true,
		closable : true,
		modal : true,
	});
	/***童星初始化设置**/
	dialogTeacherReplys = $('#dialogTeacherReplys');
	formTeacherReplys = $('#formTeacherReplys');
	name = $('#name');
	childrenNickName= $('#childrenNickName');
	childrenStarSex = $('#childrenStarSex');
	childrenStarAge= $('#childrenStarAge');
	logo = $('#logo');
	id = $('#id');
	sessionID = $('#sessionID').val();
	status= $('#status');
	createDate1 = $('#createDate1').datebox({editable:true}).datebox('activeTextArrow');
	createDate2 = $('#createDate2').datebox({editable:true}).datebox('activeTextArrow');
	name = $('#name').validatebox({required:true,editable:true,validType:['name']});
	childrenNickName=$('#childrenNickName').validatebox({required: true, validType:['name']});
	childrenStarPro=$('#childrenStarPro').combotree({required:true,editable:false,valueField : 'id', textField : 'text',url:sy.bp()+'/admin/courseType/listTreegrid',
		 delay:800,validType:"ctypeVertify" ,cascadeCheck:true,multiple:true }).combotree('activeTextArrow').combotree('initClear');
	childrenStarDream=$('#childrenStarDream').validatebox({required: true, validType:['chinese']});
	childrenStarTrain=$('#childrenStarTrain').combotree({required:true,editable:false,valueField : 'id', textField : 'text',url:sy.bp()+'/admin/pxjg/listTreegrid',
		 delay:800,validType:"ctypeVertify" ,cascadeCheck:true,multiple:true }).combotree('activeTextArrow').combotree('initClear');
	childrenStarSex.combobox({valueField:'code',textField:'name',editable:false,
		url:sy.bp() + "/admin/dictionary/listDictionary?name=性别"
	}).combobox('activeTextArrow');
	 $('#status').combobox({valueField:'code',textField:'name',editable:false,
			url:sy.bp() + "/admin/dictionary/listDictionary?name=启用状态"
		}).combobox('activeTextArrow');
	childrenStarAge.numberbox({required:true,min:0,max:150,value:1});
	
	dialogTeacherReplys.show().dialog({resizable:false,closed:true,closable:true,modal:true});

	_datasDatagrid.datagrid({
		url : sy.bp()+'/admin/user/store/list?userid=' + userid,
		fit : true,
		autoRowHeight : true,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : false,
		fitColumns : true,
		pageSize : 10,
		pageList : [10,20,30,40,50],
		nowrap : true,
		border : false,
		idField : 'id',
		sortName : 'createTime',
		sortOrder : 'desc',
		view: sy.myview,
        emptyMsg: '没有查询到相关数据',
		toolbar: [
		          {text:'批量删除',iconCls: 'icon-remove',handler: deleteRow},'-',
		          {text:'撤销',iconCls: 'icon-redo',handler: rejectRow},'-',
		          {text:'刷新',iconCls: 'icon-reload',handler: function(){_datasDatagrid.datagrid('reload');}},'-',
		          {text:'查看详情',iconCls:'icon-accept',handler:lookDetails},'-',
		         ],
		frozenColumns: [],
		columns:[[
		          {field:'id',title:'编号',align:'center',width:100,sortable:true,checkbox:true},
		          {field:'name',title:'收藏对象',width:100,align:'left'},
		          {field:'objid',title:'对象id',width:100,align:'left',hidden:true},
		          {field:'status',title:'类型',align:'center',width:100,sortable:true,
	      		        formatter:function(value, row) {
			        	  if (value ==1) {
			        		  return "机构";
			        	  } else if (value == 2) {
			        		  return "教师";
			        	  }else if (value == 3) {
			        		  return "童星";
			        	  }else {
			        		  return " ";
			        	  }
	      		       }
	               },
			       {field:'createName',title:'操作人',width:100,align:'left'},
			       {field:'createTime',title:'操作时间',width:100,align:'left'},
				]],
			onBeforeLoad : function(param){
			},
		onLoadSuccess : function(data) {
			editRowsNumber = -1;
			$.messager.progress('close');
		},
		
		onRowContextMenu : function(e, rowIndex, rowData){
			e.preventDefault();	
			_datasDatagrid.datagrid('unselectAll').datagrid('selectRow', rowIndex);
			$('#datagridContextMenu').menu('show', {left:e.pageX,top:e.pageY });
		},
	});
	$(document).keydown(function(event){
		if(event.keyCode==13){
			search_datas();
		} 
	});
});